package dymnd.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.AbstractList;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;

import dymnd.image.Layer;



public class LayerPanel extends JList{
	
	private LinkedList<LayerCard>	layerCards = new LinkedList<LayerCard>();
	
	//method for TESTING purposes only!!
	public static void main(String[] args)	{
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(200, 200);
		//f.setLayout(null);
		LayerPanel p = new LayerPanel();
		f.add(p);
		//f.setUndecorated(true);
		f.setVisible(true);
		
		p.setSize(150, 200);
		p.setLocation(0,0);
		p.addLayer(new Layer(100, 100, "Bluescales").setIndex(0));
		p.addLayer(new Layer(100, 100, "SomeOtherGuy").setIndex(1));
		p.setPreferredSize(p.getSize());
		JScrollPane sp = new JScrollPane(p);
		f.getContentPane().add(sp);
	
		
	}
	
	
	public LayerPanel()	{
		setCellRenderer(new LayerCellRenderer());
	}
	
	public void updateLayer(Layer l)	{
		for(int i = 0; i < layerCards.size(); i++)	{
			if(layerCards.get(i).layer.getId() == l.getId())	{
				layerCards.get(i).updateLayer(l);
			}
		}
	}
	
	public void updateLayers(AbstractList<Layer>	al)	{
		for(int i = 0; i < al.size(); i++)	{
			for(int j = 0; j < layerCards.size(); j++)	{
				
				//update our layer cards with latest layer data
				if(layerCards.get(j).layer.getId() == al.get(i).getId())	{
					layerCards.get(j).updateLayer(al.get(j));
					
				}
				
				//make sure all of our layers are ordered correctly. 
				if(layerCards.get(j).layer.index != j)	{
					layerCards.add(layerCards.get(j).layer.index, layerCards.remove(j));
				}
			}
		}
		
		onLayerUpdate();
		
	}
	
	private void onLayerUpdate()	{
		//reset the list in this JList, to account for any changes
		removeAll();
		setListData(layerCards.toArray(new LayerCard[layerCards.size()]));
		setPreferredSize(new Dimension(layerCards.size()*LayerCard.DEFAULT_WIDTH, LayerCard.DEFAULT_HEIGHT));
	}
	
	public void addLayer(Layer l)	{
		layerCards.add(l.index, new LayerCard(l));
		onLayerUpdate();
	}
	
	
	//Make these drag-and-droppable for the purpose of re-arranging layers using the mouse.
	protected class LayerCard extends JPanel	{
		private Layer layer = null;
		
		private JCheckBox visibleBox = new JCheckBox();
		
		private JLabel nameLabel = new JLabel();
		private JLabel ownerLabel = new JLabel();
		private JLabel previewIconLabel = new JLabel();
		
		private final Color SEPARATOR_COLOR = new Color(100, 100, 100);
		
		public static final int DEFAULT_WIDTH = 150;
		public static final int DEFAULT_HEIGHT = 40;
		
		public LayerCard(Layer l)	{
			layer = l;
			setBorder(BorderFactory.createLineBorder(new Color(127, 145, 170), 1));
			setLayout(null);
			setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
			
			previewIconLabel.setIcon(new ImageIcon(layer));
			previewIconLabel.setLocation(10, getHeight()/2 - previewIconLabel.getHeight()/2);
			previewIconLabel.setVisible(true);
			previewIconLabel.setSize(20, 20);
			add(previewIconLabel);
			
			//add a nice separator between the icon and the text
			JLabel separatorLabel = new JLabel();
			separatorLabel.setBackground(SEPARATOR_COLOR);
			separatorLabel.setSize(2, getHeight() - 20);
			separatorLabel.setLocation(previewIconLabel.getX() + previewIconLabel.getWidth() + 5, getHeight()/2 - separatorLabel.getHeight()/2);
			add(separatorLabel);
			
			
			
			nameLabel.setText(layer.getName());
			nameLabel.setLocation(previewIconLabel.getX() + previewIconLabel.getWidth() + 11, 0);
			nameLabel.setSize(100, 20);
			add(nameLabel);
			
			ownerLabel.setText("<html><i>&#60" + layer.getOwner() + "&#62;</i>");
			ownerLabel.setForeground(new Color(60, 60, 60));
			ownerLabel.setLocation(nameLabel.getX(), nameLabel.getHeight() + nameLabel.getY());
			ownerLabel.setSize(100, 20);
			add(ownerLabel);
			
			updateLayer(l);
			
		}
		
		public void updateLayer(Layer l)	{
			layer = l;
			nameLabel.setText(l.getName());
			previewIconLabel.setIcon(new ImageIcon(l));
			
			
		}
		
		/*public void paintComponent(Graphics g)	{
			g.setColor(Color.black);
			g.drawString("Layer", 20, 30);
		}*/
		
	}
	
}

class LayerCellRenderer	implements ListCellRenderer	{
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean selected, boolean hasFocus) {
		LayerPanel.LayerCard card = (LayerPanel.LayerCard)value; 
		if(selected)	{
			card.setBackground(new Color(200, 200, 200));
		}	else	{
			card.setBackground(new Color(0, 0, 0, 10));
		}
		return card;
		
	}

}

